// index.js
const auth = require('../../utils/auth.js');

const defaultAvatarUrl =
  "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";

Page({
  data: {
    motto: "Hello World",
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: "",
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse("getUserProfile"),
    canIUseNicknameComp: wx.canIUse("input.type.nickname"),
    isLoggedIn: false,
    banners: [
      { id: 1, imageUrl: "/images/banner1.png" },
      { id: 2, imageUrl: "/images/banner2.png" },
      { id: 3, imageUrl: "/images/banner3.png" },
    ],
    recommendDishes: [
      {
        id: 1,
        name: "红烧排骨",
        price: 25.0,
        rating: 4.8,
        imageUrl: "/images/dishes/dish1.png",
      },
      {
        id: 2,
        name: "宫保鸡丁",
        price: 12.0,
        rating: 4.5,
        imageUrl: "/images/dishes/dish2.png",
      },
    ],
  },

  checkLoginStatus() {
    const isLoggedIn = auth.checkLoginStatus();
    const userInfo = auth.getUserInfo();
    
    this.setData({
      isLoggedIn,
      userInfo: userInfo || this.data.userInfo,
      hasUserInfo: !!userInfo
    });
    
    return isLoggedIn;
  },

  onLoad() {
    this.checkLoginStatus();
  },

  onShow() {
    // 每次页面显示时检查登录状态
    this.checkLoginStatus();
  },

  bindViewTap() {
    wx.navigateTo({
      url: "../logs/logs",
    });
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    const { nickName } = this.data.userInfo;
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    });
  },

  onInputChange(e) {
    const nickName = e.detail.value;
    const { avatarUrl } = this.data.userInfo;
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    });
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: "展示用户信息", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res);
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
        });
      },
    });
  },

  navigateToLogin() {
    wx.navigateTo({
      url: "/pages/login/login",
    });
  },

  // 添加页面跳转方法
  goToMenu() {
    wx.switchTab({
      url: '/pages/menu/menu'
    });
  },

  goToRecommend() {
    wx.navigateTo({
      url: '/pages/recommend/recommend'
    });
  },

  goToNutrition() {
    wx.navigateTo({
      url: '/pages/nutrition/nutrition'
    });
  },

  goToCommunity() {
    wx.navigateTo({
      url: '/pages/community/community'
    });
  },

  goToDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/dish/detail?id=${id}`
    });
  },

  goToRanking() {
    wx.navigateTo({
      url: '/pages/ranking/ranking'
    });
  }
});
